// pages/package/nav_supply_approve/supply_approve.js
const mybehavior = require("../../../../../../behaviors/requests")
import Dialog from '@vant/weapp/dialog/dialog';
Page({
  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    page_size: 10,
    page: 1,
    total: 0,
    page_all:0,
    baseUrl: "https://logisticsengineering.gree.com/package_integrated_api/supply_user_manage",
    phone_num: "",
    list: [],
    sp_show: false,
    key: "",
    reason: "ok",
    action: "agree",
    qsyj_options: ["agree", "disagree"],
    agree_btn: "danger",
    disagree_btn: "default",
    caozuo_show: false,
    bh_show: false,
    bh_reason: "",
    row_data: "",
    previewImageClick: false,
    faren_photo_list: [],
    zzimg_list: [],
    hsbzqd_show: false,
    spjl_show: false,
    hsbzqd_list: [],
    spjl_list: [],
    photo_list: [],
    task_row_data: {},
    activeNamesDetail: ['1'],
    onActiveChangeStep: [],
    task_id: "",
    inspect_show: true,
    data_arr: {},

  },
  showInvolved(e) {
    console.log(e.currentTarget.dataset.row_data);
    let msg = e.currentTarget.dataset.row_data
    let lists = []
    for(let i in msg){
      lists.push('|'+msg[i].name+'|')
    }
    Dialog.alert({
      title: lists,
    }).then(() => {
      // on close
    });
  },
  onActiveDetailChange(event) {
    this.setData({
      activeNamesDetail: event.detail,
    });
  },
  onActiveChangeStep(event) {
    this.setData({
      activeNamesStep: event.detail,
    });
  },
  onChange(event) {
    this.setData({
      activeName: event.detail,
    });
  },
  onAgree() {
    this.setData({
      agree_btn: "danger",
      reason: "ok",
      action: "agree",
      disagree_btn: "default",
    })
  },
  onDisgree() {
    this.setData({
      agree_btn: "default",
      action: "disagree",
      reason: "",
      disagree_btn: "danger",
    })
  },
  // 驳回弹框
  onBhShow() {
    var bh_arr = this.data.row_data.records
    var bh_val = bh_arr[bh_arr.length - 1].reason
    console.log(bh_val)
    this.setData({
      bh_show: true,
      bh_reason: bh_val
    });
  },
  // 驳回弹框确认
  onCzConfirm() {
    this.setData({
      bh_show: false,
    });
  },
  // 驳回弹框关闭
  onCzClose() {
    this.setData({
      bh_show: false,
    });
  },
  // 操作弹框确认
  onCzConfirm() {
    this.setData({
      caozuo_show: false,
    });
  },
  // 操作弹框关闭
  onCzClose() {
    this.setData({
      caozuo_show: false,
    });
  },
  // 审批弹框确定按钮
  onSpConfirm() {
    if (this.data.reason == "" || this.data.action == "") {
      wx.showToast({
        title: '不能为空',
        icon: "error"
      })
    } else {
      wx.requestSubscribeMessage({
        tmplIds: ['Qq4EMCt24ZVzeR74sHCcdCgZyo7pus8Bf9knNoUHPpM'],
  
      })
      var url = this.data.baseUrl + "/inspect_register_outer_supply_r"
      var data = {
        key: this.data.task_row_data.task._id,
        reason: this.data.reason,
        action: this.data.action
      }
      console.log(data);
      this.post(url, data).then(res => {
        console.log("审批返回结果", res)
        if (res.code == 20000) {
          wx.showToast({
            title: res.msg,
            icon: "success"
          });
          setTimeout(function () {
            wx.reLaunch({
              url: '/packageB/pages/package/task_manager/task_manager',
            })
          }, 2000)
        } else {
          wx.showToast({
            title: res.msg,
            icon: "error"
          })
        }
      }).catch(err => {
        wx.showToast({
          title: err.msg,
          icon: 'error'
        })
      })

    }

  },
  // 审批弹框关闭
  onSpClose() {
    this.setData({
      sp_show: false,
      key: "",
      reason: "ok",
      action: "agree",
      agree_btn: "danger",
      disagree_btn: "default",
    });
  },
  // 点击审批按钮
  onApprove(e) {
    console.log(e.target.dataset.row_data);
    var row = e.target.dataset.row_data
    this.setData({
      key: row.key,
      row_data: row,
      sp_show: true,
    })
  },


  // 条件查询
  Submit() {
    var url = this.data.baseUrl + "/query_register_outer_supply_r"
    var data = {
      phone_num: this.data.phone_num,
      page_size: this.data.page_size,
      page: this.data.page,
    }
    this.post(url, data).then(res => {
      console.log("返回结果", res)
      if (res.code == 20000) {
        this.setData({
          total: res.data.total,
          page_all:Math.ceil(res.data.total/this.data.page_size),
          list: res.data.results,
          previewImageClick:true
        })
        wx.showToast({
          title: res.msg,
          icon: "success"
        });
      } else {
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  pageUp() {
    var now_page = this.data.page
    if (now_page != 1) {
      now_page = now_page - 1
      this.setData({
        page: now_page
      })
      this.getSupplyDatas()
    }


  },
  pageDown() {
    var now_page = this.data.page
    if (now_page < this.data.page_all) {
      now_page = now_page + 1
      this.setData({
        page: now_page
      })
      this.getSupplyDatas()
    }

  },
  // 查询供应商用户注册申请列表
  getSupplyDatas() {
    var url = this.data.baseUrl + "/query_register_outer_supply_r"
    var data = {
      phone_num: this.data.phone_num,
      page_size: this.data.page_size,
      page: this.data.page,
    }
    this.post(url, data).then(res => {
      console.log("返回结果", res)
      if (res.code == 20000) {
        this.setData({
          total: res.data.total,
          page_all:Math.ceil(res.data.total/this.data.page_size),
          list: res.data.results,
          previewImageClick:true
        })
        // wx.showToast({
        //   title: res.msg,
        //   icon: "success"
        // });
      } else {
        this.setData({
          list: []
        })
        wx.showToast({
          title: res.msg,
          icon: "error"
        })
      }
    }).catch(err => {
      this.setData({
        list: []
      })
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })
  },
  // 预览照片
  previewPhoto(e) {
    console.log(e.currentTarget.dataset.row_data)
    var img_url = e.currentTarget.dataset.row_data
    var url_l = []
    url_l.push(img_url)
    wx.previewImage({
      current: img_url,
      urls: url_l,
    })
  },
  // 营业执照信息显示
  onOutdoorContainers(e) {
    console.log(e.target.dataset.row_data);
    var data_arr = e.target.dataset.row_data
    var data_list = []
    data_list.push(data_arr)
    var zz_img_list = data_arr.zz_img
    var zzimg_show_list = []
    for (var i in zz_img_list) {
      zzimg_show_list.push("https://logisticsengineering.gree.com/package_integrated_api/photo_manage/" + "fs_download?file_id=" + zz_img_list[i])
    }
    var fr_img_list = data_arr.faren_ID_card
    var fr_img_show_list = []
    for (var i in fr_img_list) {
      fr_img_show_list.push("https://logisticsengineering.gree.com/package_integrated_api/photo_manage/" + "fs_download?file_id=" + fr_img_list[i])
    }
    // console.log("1", img_show_list)
    this.setData({
      zzimg_list: zzimg_show_list,
      hsbzqd_show: true,
      hsbzqd_list: data_list,
      faren_photo_list: fr_img_show_list
    })
  },
  onhsbzqdClose() {
    this.setData({
      hsbzqd_show: false
    })
  },
  onhsbzqdConfirm() {
    this.setData({
      hsbzqd_show: false
    })
  },

  // 审批记录弹出框
  onApproveRecords(e) {
    var data_list = e
    if (data_list.records.length != 0) {

      var show_list = data_list.records

      for (var i in show_list) {
        show_list[i].time = this.GMTToStr(show_list[i].time)
        if (show_list[i].action == "agree") {
          show_list[i].action = "同意"
        } else {
          show_list[i].action = "不同意"
        }
      }
    } else {

    }
    this.setData({
      spjl_show: true,
      spjl_list: show_list,
    })
  },
  onspjlClose() {
    this.setData({
      spjl_show: false
    })
  },
  onspjlConfirm() {
    this.setData({
      spjl_show: false
    })
  },

   // 时间转换
  //  GMTToStr(time) {
  //   let date = new Date(time)
  //   let Str = date.getFullYear() + '-' +
  //     (date.getMonth() + 1) + '-' +
  //     date.getDate() + ' '
  //   return Str
  // },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad(options) {
    let self = this
    const eventChannel = this.getOpenerEventChannel()
    // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
    eventChannel.on('acceptDataFromOpenerPage', function (data) {
      console.log("传过来的数据", data)
      if (data.flag == "done") {
        self.setData({
          inspect_show: false,
        })
      }
      let arr = []
      arr.push(data.task)
      let steps_arr = []
      for (let i in data.task.records) {
        data.task.records[i].time = self.GMTToStr(data.task.records[i].time)
          data.task.records[i].action = data.task.records[i].action == "agree" ? "同意" : "不同意"
        let obj = {
          text: '【' + data.task.records[i].process + '】',
          desc: data.task.records[i].name + ' 在 ' + data.task.records[i].time + ' ' + ' ' + data.task.records[i].action + '办理，意见反馈：' + data.task.records[i].reason,
        }
        steps_arr.push(obj)
      }
      steps_arr[0] = {
        text: '【编制】',
        desc: data.task.records[0].name + ' 在 ' + data.task.records[0].time + ' 提交'
      }

      self.setData({
        task_row_data: data,
        task_id: data.task_id,
        business_content: data.business_content,
        creator_name: data.creator_name,
        lastProcessUser: data.lastProcessUser,
        lastProcessAction: data.lastProcessAction,
        data_arr: data.task,
        steps: steps_arr,
        active: steps_arr.length - 1,
        list: arr


      })


    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // if (this.data.previewImageClick == false) {
    //   this.getSupplyDatas()
    // }


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})